﻿using System.Linq;
using System.Text;
using System.Collections.Generic;

namespace Web.Base.Models
{
    public sealed class ShoppingCartItem
    {
        /// <summary>
        /// 商品Id
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// 商品SkuId
        /// </summary>
        public int SkuId { get; set; }

        /// <summary>
        /// 产品Id
        /// </summary>
        public int ProductId { get; set; }

        /// <summary>
        /// 商品所属的叶子类目 id
        /// </summary>
        public int CategoryId { get; set; }

        /// <summary>
        /// 根类目Id
        /// </summary>
        public int CategoryRootId { get; set; }

        /// <summary>
        /// 子类目字符串不包含rooid categoryid
        /// </summary>
        public string CategoryMiddleId { get; set; }

        /// <summary>
        /// 商家外部编码(可与商家外部系统对接)
        /// </summary>
        public string OuterId { get; set; }

        /// <summary>
        /// 是否为虚拟物品
        /// </summary>
        public bool IsVirtual { get; set; }

        /// <summary>
        /// 套餐Id
        /// </summary>
        public int MealId { get; set; }

        /// <summary>
        /// 套餐的值 如：M8原装电池:便携支架:M8专用座充:莫凡保护袋
        /// </summary>
        public string MealName { get; set; }

        /// <summary>
        /// 商品名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 单价
        /// </summary>
        public decimal Price { get; set; }

        /// <summary>
        /// 数量
        /// </summary>
        public int Quantity { get; set; }

        /// <summary>
        /// 重量
        /// </summary>
        public decimal Weight { get; set; }

        /// <summary>
        /// 库存数量
        /// </summary>
        public int Stock { get; set; }

        /// <summary>
        /// 小计
        /// </summary>
        public decimal Total
        {
            get { return Price*Quantity - Discount; }
        }

        /// <summary>
        /// 折扣
        /// </summary>
        public decimal Discount
        {
            get
            {
                var amount = 0M;
                if (Discounts != null && Discounts.Count > 0)
                {
                    amount = Discounts.Sum(o => o.Value);
                }
                return amount*Quantity;
            }
        }

        /// <summary>
        /// 属性
        /// </summary>
        public string[] Properties { get; set; }

        /// <summary>
        /// 是否为赠品
        /// </summary>
        public bool IsGift { get; set; }

        /// <summary>
        /// 是否是服务订单 是返回true，否返回false
        /// </summary>
        public bool IsServiceOrder { get; set; }

        /// <summary>
        /// 订单中可购买最小数量
        /// </summary>
        public int OrderMinQuantity { get; set; }

        /// <summary>
        /// 订单中可购买最大数量
        /// </summary>
        public int OrderMaxQuantity { get; set; }

        /// <summary>
        /// 扩展信息
        /// </summary>
        public string Extra { get; set; }

        /// <summary>
        /// 信息
        /// </summary>
        public string Message { get; set; }

        public IDictionary<int, decimal> Discounts { get; set; }

        public ShoppingCartItem()
        {
            Discounts = new Dictionary<int, decimal>();
            OrderMaxQuantity = int.MaxValue;
            OrderMinQuantity = 0;
            Quantity = 1;
        }

        public string GetPropsName()
        {
            var sb = new StringBuilder(5);
            if (Properties != null)
            {
                foreach (var s in Properties)
                {
                    sb.Append(s + ";");
                }
                return sb.ToString().TrimEnd(';');
            }
            return string.Empty;
        }
    }
}